<template>
  <section>
    <main-bar></main-bar>
    <h3>同意获取您的计算机信息吗？</h3>
    <div>
      <ul>
        <li>
          <span>您的CPU序列号:</span>
          <span>{{computer.CPUCode}}</span>
        </li>
        <li>
          <span>您的硬盘序列号:</span>
          <span>{{computer.HardDiskCode}}</span>
        </li>
        <li>
          <span>您的主板序列号:</span>
          <span>{{computer.MainBoardCode}}</span>
        </li>
      </ul>
    </div>
    <p>说明：硬件信息只是演示</p>
    <div>
      <el-button @click='getComputer'>同意获取信息</el-button>
    </div>
  </section>
</template>
<script>
// const ipcRenderer = require('electron').ipcRenderer
const path = require('path')
const spawn = require('child_process').spawn
export default {
  name: 'hardware',
  data () {
    return {
      computer: {
        CPUCode: '',
        HardDiskCode: '',
        MainBoardCode: ''
      }
    }
  },
  methods: {
    getComputer () {
      this.$message({
        message: '获取信息中...',
        type: 'success'
      })

      // 项目根目录
      // let pathName = path.resolve(__dirname, '../../../../') // 打包里的
      // let child = spawn(pathName + '/cashAdpter/CashAdpter.Main.exe', ['hardcode'])
      // pathName = pathName.replace(/\\/g, '\\\\')

      // 相对目录
      let pathName = path.resolve(__dirname, '../../utils') // 打包里的
      let child = spawn(pathName + '/cashAdpter/CashAdpter.Main.exe', ['hardcode'])
      child.stdout.on('data', data => {
        let copy = Buffer.from(data)
        copy = JSON.parse(copy.toString())
        console.log(copy)
        this.computer = copy
        this.$message({
          message: '您计算硬件信息已读取',
          type: 'success'
        })
      })
      child.stdout.on('error', (error) => {
        this.$message({
          message: '您计算硬件信息读取失败',
          type: 'warning'
        })
        console.log(error)
      })
    }
  }
}
</script>

